<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');


/**
 * This class is for online shop order process - payment.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */
 
 /**
 * Start page
 */
session_start();

$profile = new UserProfile();
$profile->run();

class UserProfile extends Controller
{
	/**
	 * Object member info
	 */
	private $objMember;
	
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();		
	}
	
	/** 
	 * Start page function
	 * @params null
	 * @Return null
	 */
	public function run()
	{
		$this->import('Database');
		$this->import("FrontendUser", "User");
		$this->loadDataContainer("tl_member");
		
		$mainHead = "";
		if (isset($_GET['registration']) && $_GET['registration'] && $_GET['registration'] == 'success')
		{
			$mainHead = "<div class=\"result_info\">
		<p>Your account has been created!</p>
		<p>A confirmation has been sent to the provided email address. If you have any question about the operation of this online shop, please <a href=\"" . $this->Environment->base . "general-inquiries.html\" title=\"contact us\" style=\"color:#ffffff;text-decoration:underline;\">Contact Us</a>.</p>
		<p>Happy shopping. <a href=\"" . $this->Environment->base . "\" title=\"Daft shopping\" style=\"color:#ffffff;text-decoration:underline;\">Start here.</a></p>
	</div> <!-- result_info end -->";
		}
		
		// Path
		$path = "<div class=\"path\"><a title=\"HOME\" href=\"index.php\">HOME</a>&nbsp;" . htmlentities('>') . "&nbsp;<a title=\"MY ACCOUNT\" href=\"profile.html\">" . (($this->User->firstname) ? $this->User->firstname : $this->User->email) . "'s account</a></div><div class=\"quick_tool\">
					<a title=\"ORDER STATUS\" href=\"order-status.html\">ORDER STATUS</a> <a href=\"profile_detail.html\" title=\"PROFILE\">PROFILE</a> <a title=\"WISHLIST\" href=\"backpocket.html\">WISHLIST</a> <a title=\"ADDRESS BOOK\" href=\"shipping_payment.html\">ADDRESS BOOK</a>
				</div>";
		// Main Left.
		$strOderHistory = $this->displayOderHistory();
		$strBackPocket = $this->displayBackPocket();
		
		$mainLeft = "<div class=\"main_left\">" . $strOderHistory . $strBackPocket . "</div>";
		
		// Main right.
		$strProfile = $this->displayMeberProfile();
		$strShippingOrPayment = $this->displayShippingOrPayment();
		
		$mainRight = "<div class=\"main_right\">" . $strProfile . $strShippingOrPayment . "</div>";
		
		echo $path . $mainHead . $mainLeft . $mainRight;
	}
	
	/**
	 * Get order history and order status.
	 * @params null
	 * @Return string html.
	 */
	 private function displayOderHistory()
	 {
		$this->import('FrontendUser', 'User');
		$this->loadDataContainer('tl_member');
		
		// Get the order information.
		$objOrders = $this->Database->prepare("SELECT odr.id, odr.orderno, odr.date_add, odr.total_paid_real, odr_sta.name, odr.id_member, (SELECT COUNT(*) FROM tl_orders) AS cnt 
												FROM tl_orders AS odr
												LEFT JOIN tl_order_history AS odr_h ON odr_h.id_order=odr.id
												LEFT JOIN tl_order_state_lang AS odr_sta ON odr_h.id_order_state=odr_sta.id_order_state AND odr_sta.id_lang=odr.id_lang
												WHERE odr.id_member=? 
												ORDER BY date_upd DESC")
									->limit(1)
									->execute($this->User->id, time(), time());
		if ($objOrders->numRows < 1)
		{
			$strHtml = "<div class=\"box order\">
			<h2>There's no transaction.</h2>
			<h3>ORDER STATUS / HISTORY</h3>
			<div class=\"cons\">
				There are no orders for this user.
			</div> <!-- cons end -->
			<!--<p class=\"more\"><a href=\"order-status.html\" title=\"more detail\">more details</a></p>-->
		</div> <!-- box end -->";
		
			return $strHtml;
		}
		
		$strHtml = "<div class=\"box order\">
			<h2>There's " . $objOrders->cnt . " transaction.</h2>
			<h3>ORDER STATUS / HISTORY</h3>
			<div class=\"cons\">
				<table>
					<tr>
						<td class=\"order_date\">ORDER DATE</td>
						<td class=\"order_number\">ORDER NUMBER</td>
						<td class=\"order_status\">STATUS</td>
					</tr>
					<tr>
						<td>" . $objOrders->date_add . "</td>
						<td>" . $objOrders->orderno . "</td>
						<td>" . $objOrders->name . "</td>
					</tr>
				</table>
			</div> <!-- cons end -->
			<p class=\"more\"><a href=\"order-status.html\" title=\"more detail\">more details</a></p>
		</div> <!-- box end -->";
		
		return $strHtml;
	 }
	 
	 /**
	  * Get the BackPocet(from session).
	  * @params null
	  * @Return string.
	  */
	 private function displayBackPocket()
	 {
		$strHtml = "<div class=\"box backpocket\">
			<h2>Feeling indicisive?</h2>
			<h3>WISHLIST</h3>
			<div class=\"cons\">
				<p>Feeling indicisive? Click \"Slip in Wishlist\" and we'll save the items beneath the shopping cart. Add them back to your cart later, but don't wait too long, merchandise can sell out fast! </p>
			</div> <!-- cons end -->
			<p class=\"more\"><a href=\"backpocket.html\" title=\"check WISHLIST\">check WISHLIST</a></p>
		</div> <!-- box end -->";
		
		return $strHtml;
	 }
	 
	 /**
	  * Get the member information.
	  * @params null
	  * @Return string
	  */
	 private function displayMeberProfile()
	 {
		/** 
		 * Get user hash and session
		 */
		$objSession = $this->Database->prepare("SELECT * FROM tl_session WHERE hash=? AND name=?")
									 ->execute($_COOKIE['FE_USER_AUTH'], 'FE_USER_AUTH');
			
		if ($objSession->numRows < 1)
			return;
				
		/**
		 * Get default address of billing
		 */
		$this->objMember = $this->Database->prepare("SELECT * FROM tl_member WHERE id=?")
									->execute($objSession->pid);
		if ($this->objMember->numRows < 1)
				return;	
		
		$strHtml = "<div class=\"box profile\">
			<h2>Manage your account profile and email settings.</h2>
			<h3>PROFILE</h3>
			<div class=\"cons\">
				<table>
					<tr>
						<td class=\"td1\">EMAIL:</td>
						<td class=\"td2\">" . $this->objMember->email . "</td>
					</tr>
					<tr>
						<td>PASSWORD: </td>
						<td>********</td>
					</tr>
				</table>
			</div> <!-- cons end -->
			<p class=\"more\"><a href=\"profile_detail.html\" title=\"edit account info\">edit account info</a></p>
		</div> <!-- box end -->";
		
		return $strHtml;
	 }
	 
	 /**
	  * Get shipping and payment address
	  * @params null
	  * @Return string
	  */
	 private function displayShippingOrPayment()
	 {
		$objAddressBook = $this->Database->prepare("SELECT * FROM tl_address_book WHERE uid=?")
									->execute($this->objMember->id);
		
		if ($objAddressBook->numRows < 1)
		{
			$strHtml = "<div class=\"box info\">
			<h2>Store shipping and payment info for faster checkout</h2>
			<h3>ADDRESS BOOK</h3><div class=\"cons\"><div class=\"item\">There no shipping address.</div></div> <!-- cons end --></div> <!-- box end -->";
			
			return $strHtml;
		}
		
		$strHtml = "<div class=\"box info\">
			<h2>Store shipping and payment info for faster checkout</h2>
			<h3>ADDRESS BOOK</h3>
			<div class=\"cons\">";
		
		$index = 1;
		while ($objAddressBook->next())
		{
			$strHtml .= "<div class=\"item\"><p>#" . $index . "&nbsp;" . $objAddressBook->street . "</p></div>";
			
			$index++;
		}
		
					
		$strHtml .= "</div> <!-- cons end -->
			<p class=\"more\"><a href=\"shipping_payment.html\" title=\"view or edit\">view or edit</a></p>
		</div> <!-- box end -->";
		
		return $strHtml;
	 }
}
?>